Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

smob

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smob

Zero dependency library to safe merge objects.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2M
decreased by-18.15%
Maintainers
1
Weekly downloads
 
Created
Source

SMOB 🧪

npm version main codecov Known Vulnerabilities semantic-release: angular

Zero dependency library to safe merge objects.

Table of Contents

Installation

npm install smob --save

Usage

import { merge } from "smob";

const ob = merge(target, ...sources);

The following merge options are set by default:

  • priority: left The source aka leftmost object has by default the highest priority.
  • array: true Merge source and target arrays by priority order.
  • arrayDistinct: false Remove duplicates, when merging array properties.

The merge behaviour can be changed by creating a custom merger.

Arguments

  • target Record<string, any>: The destination object to merge the source object(s).
  • sources Record<string, any>[]: The source object(s).
import { merge } from 'smob';

console.log(merge({ a: 1 }, { a: 2 }, { a: 3 }));
// => { a: 1 }

Merger

A custom merger can simply be created by using the createMerger method.

Priority

import { createMerger } from 'smob';

const merge = createMerger({ priority: 'right' });

console.log(merge({ a: 1 }, { a: 2 }, { a: 3 }));
// => { a: 3 }

Array

import { createMerger } from 'smob';

const merge = createMerger({ array: false });

console.log(merge({ a: [1,2,3] }, { a: [4,5,6] }));
// => { a: [1,2,3] }

ArrayDistinct

import { createMerger } from 'smob';

const merge = createMerger({ arrayDistinct: true });

console.log(merge({ a: [1,2,3] }, { a: [3,4,5] }));
// => { a: [1,2,3,4,5] }

Strategy

import { createMerger } from 'smob';

const merge = createMerger({
    strategy: (target, key, value) => {
        if (
            typeof target[key] === 'number' &&
            typeof value === 'number'
        ) {
            target[key] += value;
            return target;
        }

        return undefined;
    }
});

console.log(merge({ a: 1 }, { a: 2 }, { a: 3 }));
// => { a: 6 }

Return undefined if the default merge behaviour should be used for the current target source pair.

License

Made with 💚

Published under MIT License.

Keywords

FAQs

Package last updated on 27 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc